home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2006 May / PCWMAY06.iso / Software / Toolkit / Songbird 0.1 / Songbird_0_1_0.exe / chrome / content / sbIMediaLibrary.js < prev    next >
Encoding:
Text File  |  2005-11-23  |  16.9 KB  |  552 lines

  1. /*
  2.  * Software Copyright ⌐ 2005 Pioneers of the Inevitable
  3.  *
  4.  * songbird@songbirdnest.com - <chirp, chirp>
  5.  *
  6.  * Q Public License v1.0
  7.  * 
  8.  * Copyright ⌐ 1999 Trolltech AS, Norway.
  9.  * 
  10.  * Everyone is permitted to copy and distribute this license document.
  11.  * 
  12.  * The intent of this license is to establish freedom to share and change the software 
  13.  * regulated by this license under the open source model.
  14.  * 
  15.  * This license applies to any software containing a notice placed by the copyright holder 
  16.  * saying that it may be distributed under the terms of the Q Public License version 1.0. 
  17.  * Such software is herein referred to as the Software. This license covers modification  
  18.  * and distribution of the Software, use of third-party application programs based on the 
  19.  * Software, and development of free software which uses the Software.
  20.  */
  21. //
  22. // sbIMediaLibrary Object
  23. //
  24. const SONGBIRD_MEDIALIBRARY_CONTRACTID = "@songbird.org/Songbird/MediaLibrary;1";
  25. const SONGBIRD_MEDIALIBRARY_CLASSNAME = "Songbird Media Library Interface";
  26. const SONGBIRD_MEDIALIBRARY_CID = Components.ID("{ca5c9b2c-e061-4c98-8333-9c839efc0d7f}");
  27. const SONGBIRD_MEDIALIBRARY_IID = Components.interfaces.sbIMediaLibrary;
  28.  
  29. const LIBRARY_TABLE_NAME = "library";
  30. const LIBRARY_DESC_TABLE_NAME = "library_desc";
  31.  
  32. const LIBRARY_DESC_TABLE_CREATE = "CREATE TABLE library_desc (column_name TEXT, readable_name TEXT, is_visible INTEGER(0, 1) DEFAULT 0, default_visibility INTEGER(0, 1) DEFAULT 0, is_metadata INTEGER(0, 1) DEFAULT 0, sort_weight INTEGER DEFAULT 0, width INTEGER DEFAULT 0)";
  33. const LIBRARY_TABLE_CREATE = "CREATE TABLE library (id INTEGER PRIMARY KEY AUTOINCREMENT, uuid BLOB, service_uuid BLOB, url TEXT, content_type TEXT, length TEXT, artist TEXT, title TEXT, album TEXT, genre TEXT, composer TEXT, producer TEXT, rating INTEGER, track_no INTEGER, track_total INTEGER, disc_no INTEGER, disc_total INTEGER, year INTEGER)";
  34.  
  35. function CMediaLibrary()
  36. {
  37. }
  38.  
  39. /* the CMediaLibrary class def */
  40. CMediaLibrary.prototype = 
  41. {
  42.   m_queryObject: null,
  43.   
  44.   SetQueryObject: function(queryObj)
  45.   {
  46.     this.m_queryObject = queryObj; 
  47.     return;
  48.   },
  49.   
  50.   GetQueryObject: function()
  51.   {
  52.     return this.m_queryObject;
  53.   },
  54.  
  55.   CreateDefaultLibrary: function()
  56.   {
  57.     if(this.m_queryObject != null)
  58.     {
  59.       this.m_queryObject.ResetQuery();
  60.       
  61.       //Create the Library Description Table.
  62.       this.m_queryObject.AddQuery(LIBRARY_DESC_TABLE_CREATE);
  63.       
  64.       //Create the Library Table.
  65.       this.m_queryObject.AddQuery(LIBRARY_TABLE_CREATE);
  66.       
  67.       //Fill the Library Description Table.
  68.       this.m_queryObject.AddQuery("INSERT INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"id\", \"#\", 0, 0, 0, 0)");
  69.       this.m_queryObject.AddQuery("INSERT INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"uuid\", \"Media UUID\", 1, 0, 0, 0)");
  70.       this.m_queryObject.AddQuery("INSERT INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"service_uuid\", \"Service UUID\", 1, 0, 0, 0)");
  71.       this.m_queryObject.AddQuery("INSERT INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"url\", \"Location\", 1, 0, 1, 0)");
  72.       this.m_queryObject.AddQuery("INSERT INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"content_type\", \"Content Type\", 1, 0, 1, 0)");
  73.       this.m_queryObject.AddQuery("INSERT INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"length\", \"Length\", 1, 1, 1, 0)");
  74.       this.m_queryObject.AddQuery("INSERT INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"artist\", \"Artist\", 1, 1, 1, 0)");
  75.       this.m_queryObject.AddQuery("INSERT INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"title\", \"Title\", 1, 1, 1, 0)");
  76.       this.m_queryObject.AddQuery("INSERT INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"album\", \"Album\", 1, 1, 1, 0)");
  77.       this.m_queryObject.AddQuery("INSERT INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"genre\", \"Genre\", 1, 1, 1, 0)");
  78.       this.m_queryObject.AddQuery("INSERT INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"composer\", \"Composer\", 1, 0, 1, 0)");
  79.       this.m_queryObject.AddQuery("INSERT INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"producer\", \"Producer\", 1, 0, 1, 0)");
  80.       this.m_queryObject.AddQuery("INSERT INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"rating\", \"Rating\", 1, 1, 1, 0)");
  81.       this.m_queryObject.AddQuery("INSERT INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"track_no\", \"Track No.\", 1, 1, 1, 0)");
  82.       this.m_queryObject.AddQuery("INSERT INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"track_total\", \"Total No. of Tracks\", 1, 0, 1, 0)");
  83.       this.m_queryObject.AddQuery("INSERT INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"disc_no\", \"Disc No.\", 1, 0, 1, 0)");
  84.       this.m_queryObject.AddQuery("INSERT INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"disc_total\", \"Total No. of Discs\", 1, 0, 1, 0)");
  85.       this.m_queryObject.AddQuery("INSERT INTO " + LIBRARY_DESC_TABLE_NAME + " VALUES (\"year\", \"Year\", 1, 0, 1, 0)");
  86.       
  87.       this.m_queryObject.Execute();
  88.       this.m_queryObject.WaitForCompletion();
  89.     }
  90.     
  91.     return;
  92.   },
  93.  
  94.   AddMedia: function(strMediaURL, nMetaKeyCount, aMetaKeys, nMetadataValueCount, aMetadataValues, bWillRunLater)
  95.   {
  96.     if(this.m_queryObject != null)
  97.     {
  98.       aUUIDGenerator = Components.classes["@mozilla.org/uuid-generator;1"];
  99.       aUUIDGenerator = aUUIDGenerator.createInstance();
  100.       aUUIDGenerator = aUUIDGenerator.QueryInterface(Components.interfaces.nsIUUIDGenerator);
  101.   
  102.       var guid = aUUIDGenerator.generateUUID();
  103.       
  104.       var i = 0;
  105.       var strQuery = "INSERT INTO " + LIBRARY_TABLE_NAME + " (uuid, service_uuid, url, ";
  106.       for(; i < nMetaKeyCount; i++)
  107.       {
  108.         strQuery += aMetaKeys[i]
  109.         
  110.         if(i != nMetaKeyCount - 1)
  111.           strQuery += ", ";
  112.       }
  113.       
  114.       strQuery += ") VALUES (\"" + guid + "\", \"" + this.m_queryObject.GetDatabaseGUID() + "\", \"" + strMediaURL + "\", ";
  115.       
  116.       for(i = 0; i < nMetadataValueCount; i++)
  117.       {
  118.         strQuery += "\"" + aMetadataValues[i] + "\"";
  119.         
  120.         if(i != nMetadataValueCount - 1)
  121.           strQuery += ", ";
  122.       }
  123.       
  124.       strQuery += ")";
  125.       
  126.       if(!bWillRunLater)
  127.         this.m_queryObject.ResetQuery();
  128.       
  129.       this.m_queryObject.AddQuery(strQuery);
  130.       
  131.       if(!bWillRunLater)
  132.       {
  133.         this.m_queryObject.Execute();
  134.         this.m_queryObject.WaitForCompletion();
  135.       }
  136.       
  137.       return true;
  138.     }
  139.     
  140.     return false;
  141.   },
  142.   
  143.   RemoveMediaByURL: function(strMediaURL, bWillRunLater)
  144.   {
  145.     if(this.m_queryObject != null)
  146.     {
  147.       this.m_queryObject.AddQuery("DELETE FROM " + LIBRARY_TABLE_NAME + " WHERE url = \"" + strMediaURL + "\"");
  148.       
  149.       if(!bWillRunLater)
  150.       {
  151.         this.m_queryObject.Execute();
  152.         this.m_queryObject.WaitForCompletion();
  153.       }
  154.       
  155.       return true;
  156.     }
  157.     
  158.     return false;
  159.   },
  160.   
  161.   RemoveMediaByGUID: function(mediaGUID, bWillRunLater)
  162.   {
  163.     if(this.m_queryObject != null)
  164.     {
  165.       this.m_queryObject.AddQuery("DELETE FROM " + LIBRARY_TABLE_NAME + " WHERE uuid = \"" + mediaGUID +"\"");
  166.       
  167.       if(!bWillRunLater)
  168.       {
  169.         this.m_queryObject.Execute();
  170.         this.m_queryObject.WaitForCompletion();
  171.       }
  172.       
  173.       return true;
  174.     }
  175.     
  176.     return false;
  177.   },
  178.   
  179.   FindByGUID: function(mediaGUID)
  180.   {
  181.     if(this.m_queryObject != null)
  182.     {
  183.       this.m_queryObject.ResetQuery();
  184.       this.m_queryObject.AddQuery("SELECT url FROM " + LIBRARY_TABLE_NAME + " WHERE uuid = \"" + mediaGUID + "\"");
  185.       
  186.       this.m_queryObject.Execute();
  187.       this.m_queryObject.WaitForCompletion();
  188.       
  189.       var resObj = this.m_queryObject.GetResultObject();
  190.       return resObj.GetRowCell(0, 0);
  191.     }
  192.     
  193.     return "";
  194.   },
  195.   
  196.   FindByURL: function(strURL)
  197.   {
  198.     if(this.m_queryObject != null)
  199.     {
  200.       this.m_queryObject.ResetQuery();
  201.       this.m_queryObject.AddQuery("SELECT uuid FROM " + LIBRARY_TABLE_NAME + " WHERE url = \"" + strURL + "\"");
  202.       
  203.       this.m_queryObject.Execute();
  204.       this.m_queryObject.WaitForCompletion();
  205.       
  206.       var resObj = this.m_queryObject.GetResultObject();
  207.       return resObj.GetRowCell(0, 0);
  208.     }
  209.     
  210.     return "";
  211.   },
  212.  
  213.   GetMetadataFields: function(nMetadataFieldCount)
  214.   {
  215.     var aMetadataFields = new Array();
  216.     nMetadataFieldCount = 0;
  217.     
  218.     if(this.m_queryObject != null)
  219.     {
  220.       this.m_queryObject.ResetQuery();
  221.       this.m_queryObject.AddQuery("SELECT column_names FROM " + LIBRARY_DESC_TABLE_NAME + " WHERE is_metadata = 1");
  222.       
  223.       this.m_queryObject.Execute();
  224.       this.m_queryObject.WaitForCompletion();
  225.  
  226.       var resObj = this.m_queryObject.GetResultObject();
  227.       nMetadataFieldCount = resObj.GetRowCount();
  228.       
  229.       for(var i = 0; i < nMetadataFieldCount; i++)
  230.       {
  231.         aMetadataFields.push(resObj.GetRowCell(i, 0));
  232.       }
  233.     }
  234.     
  235.     return aMetadataFields;
  236.   },
  237.   
  238.   AddMetadataField: function(strField, bWillRunLater)
  239.   {
  240.     if(this.m_queryObject != null)
  241.     {
  242.       if(bWillRunLater)
  243.       {
  244.       }
  245.     }
  246.     
  247.     return;
  248.   },
  249.   
  250.   DeleteMetadataField: function(strField, bWillRunLater)
  251.   {
  252.     if(this.m_queryObject != null)
  253.     {
  254.       if(bWillRunLater)
  255.       {
  256.       }
  257.     }
  258.     
  259.     return;
  260.   },
  261.   
  262.   SetValueByIndex: function(mediaIndex, strField, strValue, bWillRunLater)
  263.   {
  264.     if(this.m_queryObject != null)
  265.     {
  266.       if(!bWillRunLater)
  267.         this.m_queryObject.ResetQuery();
  268.       
  269.       this.m_queryObject.AddQuery("UPDATE " + LIBRARY_TABLE_NAME + " SET " + strField + " = \"" + strValue + "\" WHERE id = \"" + mediaIndex + "\"");
  270.       
  271.       if(!bWillRunLater)
  272.       {
  273.         this.m_queryObject.Execute();
  274.         this.m_queryObject.WaitForCompletion();
  275.       }
  276.       
  277.       return true;
  278.     }
  279.     
  280.     return false;
  281.   },
  282.   
  283.   SetValuesByIndex: function(mediaIndex, nMetaKeyCount, aMetaKeys, nMetaValueCount, aMetaValues, bWillRunLater)
  284.   {
  285.     if(this.m_queryObject != null ||
  286.        nMetaKeyCount != nMetaValueCount)
  287.     {
  288.       if(!bWillRunLater)
  289.         this.m_queryObject.ResetQuery();
  290.       
  291.       var strQuery = "UPDATE " + LIBRARY_TABLE_NAME + " SET ";
  292.       var i = 0;
  293.       for(; i < nMetaKeyCount; i++)
  294.       {
  295.         strQuery += aMetaKeys[i] + " = \"" + aMetaValues[i] + "\"";
  296.         if(i < nMetaKeyCount - 1)
  297.           strQuery += ", ";
  298.       }
  299.       
  300.       strQuery += " WHERE id = \"" + mediaIndex + "\"";
  301.       this.m_queryObject.AddQuery(strQuery);
  302.       
  303.       if(!bWillRunLater)
  304.       {
  305.         this.m_queryObject.Execute();
  306.         this.m_queryObject.WaitForCompletion();
  307.       }
  308.       
  309.       return true;
  310.     }
  311.     
  312.     return false;
  313.   },
  314.   
  315.   GetValueByIndex: function(mediaIndex, strField)
  316.   {
  317.     if(this.m_queryObject != null)
  318.     {
  319.       this.m_queryObject.ResetQuery();
  320.       this.m_queryObject.AddQuery("SELECT " + strField + " FROM " + LIBRARY_TABLE_NAME + " WHERE id = \"" + mediaIndex + "\"");
  321.       
  322.       this.m_queryObject.Execute();
  323.       this.m_queryObject.WaitForCompletion();
  324.       
  325.       var resObj = this.m_queryObject.GetResultObject();
  326.       return resObj.GetRowCell(0, 0);
  327.     }
  328.     
  329.     return "";
  330.   },
  331.   
  332.   GetValuesByIndex: function(mediaIndex, nMetaKeyCount, aMetaKeys, nMetadataValueCount)
  333.   {
  334.     nMetadataValueCount = 0;
  335.     var aMetadataValues = new Array();
  336.     
  337.     if(this.m_queryObject != null)
  338.     {
  339.       var strQuery = "SELECT ";
  340.       this.m_queryObject.ResetQuery();
  341.       
  342.       var i = 0;
  343.       for( ; i < nMetaKeyCount; i++)
  344.       {
  345.         strQuery += aMetaKeys[i];
  346.         
  347.         if(i < nMetaKeyCount - 1)
  348.           strQuery += ", ";
  349.       }
  350.       
  351.       strQuery += " FROM " + LIBRARY_TABLE_NAME + " WHERE id = \"" + mediaIndex + "\"";
  352.       this.m_queryObject.AddQuery(strQuery);
  353.       
  354.       this.m_queryObject.Execute();
  355.       this.m_queryObject.WaitForCompletion();
  356.       
  357.       var resObj = this.m_queryObject.GetResultObject();
  358.       nMetadataValueCount = resObj.GetColumnCount();
  359.       
  360.       for(var i = 0; i < nMetadataValueCount; i++)
  361.       {
  362.         aMetadataValues.push(resObj.GetRowCell(0, i));
  363.       }
  364.     }
  365.     
  366.     return aMetadataValues;
  367.   },
  368.   
  369.   SetValueByGUID: function(mediaGUID, strField, strValue, bWillRunLater)
  370.   {
  371.     if(this.m_queryObject != null)
  372.     {
  373.       if(!bWillRunLater)
  374.         this.m_queryObject.ResetQuery();
  375.       
  376.       this.m_queryObject.AddQuery("UPDATE " + LIBRARY_TABLE_NAME + " SET " + strField + " = \"" + strValue + "\" WHERE uuid = \"" + mediaGUID + "\"");
  377.       
  378.       if(!bWillRunLater)
  379.       {
  380.         this.m_queryObject.Execute();
  381.         this.m_queryObject.WaitForCompletion();
  382.       }
  383.     }
  384.     
  385.     return;
  386.   },
  387.  
  388.   SetValuesByGUID: function(mediaGUID, nMetaKeyCount, aMetaKeys, nMetaValueCount, aMetaValues, bWillRunLater)
  389.   {
  390.     if(this.m_queryObject != null ||
  391.        nMetaKeyCount != nMetaValueCount)
  392.     {
  393.       if(!bWillRunLater)
  394.         this.m_queryObject.ResetQuery();
  395.       
  396.       var strQuery = "UPDATE " + LIBRARY_TABLE_NAME + " SET ";
  397.       var i = 0;
  398.       for(; i < nMetaKeyCount; i++)
  399.       {
  400.         strQuery += aMetaKeys[i] + " = \"" + aMetaValues[i] + "\"";
  401.         if(i < nMetaKeyCount - 1)
  402.           strQuery += ", ";
  403.       }
  404.       
  405.       strQuery += " WHERE uuid = \"" + mediaGUID + "\"";
  406.       this.m_queryObject.AddQuery(strQuery);
  407.       
  408.       if(!bWillRunLater)
  409.       {
  410.         this.m_queryObject.Execute();
  411.         this.m_queryObject.WaitForCompletion();
  412.       }
  413.       
  414.       return true;
  415.     }
  416.     
  417.     return false;
  418.   },
  419.   
  420.   GetValueByGUID: function(mediaGUID, strField)
  421.   {
  422.     if(this.m_queryObject != null)
  423.     {
  424.       this.m_queryObject.ResetQuery();
  425.       this.m_queryObject.AddQuery("SELECT " + strField + " FROM " + LIBRARY_TABLE_NAME + " WHERE uuid = \"" + mediaGUID + "\"");
  426.       
  427.       this.m_queryObject.Execute();
  428.       this.m_queryObject.WaitForCompletion();
  429.       
  430.       var resObj = this.m_queryObject.GetResultObject();
  431.       return resObj.GetRowCell(0, 0);
  432.     }
  433.     
  434.     return;
  435.   },
  436.   
  437.   GetValuesByGUID: function(mediaGUID, nMetaKeyCount, aMetaKeys, nMetadataValueCount)
  438.   {
  439.     nMetadataValueCount = 0;
  440.     var aMetadataValues = new Array();
  441.     
  442.     if(this.m_queryObject != null)
  443.     {
  444.       var strQuery = "SELECT ";
  445.       this.m_queryObject.ResetQuery();
  446.       
  447.       var i = 0;
  448.       for( ; i < nMetaKeyCount; i++)
  449.       {
  450.         strQuery += aMetaKeys[i];
  451.         
  452.         if(i < nMetaKeyCount - 1)
  453.           strQuery += ", ";
  454.       }
  455.       
  456.       strQuery += " FROM " + LIBRARY_TABLE_NAME + " WHERE uuid = \"" + mediaGUID + "\"";
  457.       this.m_queryObject.AddQuery(strQuery);
  458.       
  459.       this.m_queryObject.Execute();
  460.       this.m_queryObject.WaitForCompletion();
  461.       
  462.       var resObj = this.m_queryObject.GetResultObject();
  463.       nMetadataValueCount = resObj.GetColumnCount();
  464.       
  465.       for(var i = 0; i < nMetadataValueCount; i++)
  466.       {
  467.         aMetadataValues.push(resObj.GetRowCell(0, i));
  468.       }
  469.     }
  470.     
  471.     return aMetadataValues;
  472.   },
  473.  
  474.   QueryInterface: function(iid)
  475.   {
  476.       if (!iid.equals(Components.interfaces.nsISupports) &&
  477.           !iid.equals(SONGBIRD_MEDIALIBRARY_IID))
  478.           throw Components.results.NS_ERROR_NO_INTERFACE;
  479.       return this;
  480.   }
  481. };
  482.  
  483. /**
  484.  * \class sbMediaLibraryModule
  485.  * \brief 
  486.  */
  487. var sbMediaLibraryModule = 
  488. {
  489.   //firstTime: true,
  490.   
  491.   registerSelf: function(compMgr, fileSpec, location, type)
  492.   {
  493.     //if(this.firstTime)
  494.     //{
  495.       //this.firstTime = false;
  496.       //throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
  497.     //}
  498.  
  499.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  500.     compMgr.registerFactoryLocation(SONGBIRD_MEDIALIBRARY_CID, 
  501.                                     SONGBIRD_MEDIALIBRARY_CLASSNAME, 
  502.                                     SONGBIRD_MEDIALIBRARY_CONTRACTID, 
  503.                                     fileSpec, 
  504.                                     location,
  505.                                     type);
  506.   },
  507.  
  508.   getClassObject: function(compMgr, cid, iid) 
  509.   {
  510.     if(!cid.equals(SONGBIRD_MEDIALIBRARY_CID))
  511.         throw Components.results.NS_ERROR_NO_INTERFACE;
  512.  
  513.     if(!iid.equals(Components.interfaces.nsIFactory))
  514.         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  515.  
  516.     return sbMediaLibraryFactory;
  517.   },
  518.  
  519.   canUnload: function(compMgr)
  520.   { 
  521.     return true; 
  522.   }
  523. };
  524.  
  525. /**
  526.  * \class sbMediaLibraryFactory
  527.  * \brief 
  528.  */
  529. var sbMediaLibraryFactory =
  530. {
  531.   createInstance: function(outer, iid)
  532.   {
  533.     if (outer != null)
  534.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  535.  
  536.     if (!iid.equals(SONGBIRD_MEDIALIBRARY_IID) &&
  537.         !iid.equals(Components.interfaces.nsISupports))
  538.         throw Components.results.NS_ERROR_INVALID_ARG;
  539.  
  540.     return (new CMediaLibrary()).QueryInterface(iid);
  541.   }
  542. }; //sbMediaLibraryFactory
  543.  
  544. /**
  545.  * \function NSGetModule
  546.  * \brief 
  547.  */
  548. function NSGetModule(comMgr, fileSpec)
  549.   return sbMediaLibraryModule;
  550. } //NSGetModule
  551.